package com.sxj.shop.servlet;

import com.sxj.shop.exception.RequestParameterErrorException;
import com.sxj.shop.exception.UserNotLoginException;
import com.sxj.shop.factory.ObjectFactory;
import com.sxj.shop.service.CartService;
import com.sxj.shop.vo.Cart;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("*.cart")
public class CartServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        request.setCharacterEncoding("utf-8");
//        response.setContentType("text/html;charset=utf-8");
        String path = request.getServletPath();
        switch (path) {
            case "/add.cart":
                addCart(request, response);
                break;
            case "/modify.cart":
                modifyCart(request, response);
                break;
            case "/removeById.cart":
                removeById(request, response);
                break;
            case "/removeByIds.cart":
                removeByIds(request, response);
                break;
            case "/clean.cart":
                clean(request, response);
                break;
            default:
                PrintWriter out = response.getWriter();
                out.println("<h1 style='color:red;'>对不起,您访问的资源不存在,请重新访问</h1>");
                break;
        }
    }

    protected void addCart(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        CartService cartService = (CartService) ObjectFactory.getObject("cartService");
        String id = request.getParameter("id");
        // 从session中获取购物车
        Cart cart = (Cart) request.getSession().getAttribute("cart");

        try {
            cartService.addCart(cart,id);
            response.sendRedirect(request.getContextPath()+"/findAll.product");
        } catch (UserNotLoginException e) {
            request.setAttribute("loginMsg",e.getMessage());
            request.getRequestDispatcher("/jsp/login.jsp").forward(request,response);
        } catch (RequestParameterErrorException e) {
            request.setAttribute("msg",e.getMessage());
            request.getRequestDispatcher("/findAll.product").forward(request,response);
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg","服务器内部异常");
            request.getRequestDispatcher("/findAll.product").forward(request,response);
        }
    }

    protected void modifyCart(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        CartService cartService = (CartService) ObjectFactory.getObject("cartService");
        String id = request.getParameter("id");
        String num = request.getParameter("num");
        Cart cart = (Cart) request.getSession().getAttribute("cart");

        try {
            cartService.modifyCart(cart,id,num);
            response.sendRedirect(request.getContextPath()+"/jsp/cart.jsp");
        } catch (UserNotLoginException e) {
            request.setAttribute("loginMsg",e.getMessage());
            request.getRequestDispatcher("/jsp/login.jsp").forward(request,response);
        } catch (RequestParameterErrorException e) {
            request.setAttribute("cartMsg",e.getMessage());
            request.getRequestDispatcher("/jsp/cart.jsp").forward(request,response);
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("cartMsg","服务器内部异常");
            request.getRequestDispatcher("/jsp/cart.jsp").forward(request,response);
        }
    }

    protected void removeById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        CartService cartService = (CartService) ObjectFactory.getObject("cartService");
        String id = request.getParameter("id");
        Cart cart = (Cart) request.getSession().getAttribute("cart");

        try {
            cartService.removeCart(cart,id);
            response.sendRedirect(request.getContextPath()+"/jsp/cart.jsp");
        } catch (UserNotLoginException e) {
            request.setAttribute("loginMsg",e.getMessage());
            request.getRequestDispatcher("/jsp/login.jsp").forward(request,response);
        } catch (RequestParameterErrorException e) {
            request.setAttribute("cartMsg",e.getMessage());
            request.getRequestDispatcher("/jsp/cart.jsp").forward(request,response);
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("cartMsg","服务器内部异常");
            request.getRequestDispatcher("/jsp/cart.jsp").forward(request,response);
        }
    }

    protected void removeByIds(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        CartService cartService = (CartService) ObjectFactory.getObject("cartService");
        String[] ids = request.getParameterValues("id");
        Cart cart = (Cart) request.getSession().getAttribute("cart");

        try {
            for(String id : ids){
                cartService.removeCart(cart,id);
            }
            response.sendRedirect(request.getContextPath()+"/jsp/cart.jsp");
        } catch (UserNotLoginException e) {
            request.setAttribute("loginMsg",e.getMessage());
            request.getRequestDispatcher("/jsp/login.jsp").forward(request,response);
        } catch (RequestParameterErrorException e) {
            request.setAttribute("cartMsg",e.getMessage());
            request.getRequestDispatcher("/jsp/cart.jsp").forward(request,response);
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("cartMsg","服务器内部异常");
            request.getRequestDispatcher("/jsp/cart.jsp").forward(request,response);
        }
    }

    protected void clean(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getSession().setAttribute("cart",new Cart());
        response.sendRedirect(request.getContextPath()+"/jsp/cart.jsp");
    }
}
